package br.com.bean;


import javax.enterprise.context.ApplicationScoped;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.inject.Named;

@Named
@ApplicationScoped
public class SecurityBean extends GenericBean implements  PhaseListener {
	
	/**************************************************************************/
	/**                              ATRIBUTOS                               **/
	/**************************************************************************/
	 
	private static final long serialVersionUID = 1L;
	
	/**************************************************************************/
	/**                               M�TODOS                                **/
	/**************************************************************************/

	/** FILTRO DE URL **/
	public void afterPhase(PhaseEvent event) {
		  try {
		
			/** RECUPERA SESSION BEAN **/		

			  SessionBean sessionBean = (SessionBean) getManagedBean("sessionBean");		
			boolean isLoginPage     =  getPageAtual().contains("pageLogin");
				    
			/** NAO EXISTE USUARIO LOGADO **/
			if (sessionBean.getUsuarioLogado() == null) {
				if (!isLoginPage ) {
					goPage("pageLogin");
				}
			}
			
			
			
			
			
			/** VERIFICA BROWSER **/
			if (isInternetExplorer()) {
				/** NADA A FAZER **/
			}
		
		} catch (Exception e) {
			MessageBean.showAlertMessage(e.getMessage());
		}
	}
	
	/** VERIFICA SE � INTERNET EXPLORER **/
	private Boolean isInternetExplorer()  {
		String userAgent = getExternalContext().getRequestHeaderMap().get("user-agent");
		return userAgent.toLowerCase().indexOf("msie") > 0;
	}
		 
	 
	public PhaseId getPhaseId() {
	  return PhaseId.RESTORE_VIEW;
	}

	@Override
	public void beforePhase(PhaseEvent arg0) {
		
	}
	
	 
}
